package com.talent.proxy.mysql.common.packets.s2c;

import java.nio.ByteBuffer;

import com.talent.aio.common.ChannelContext;
import com.talent.proxy.mysql.common.MysqlExt;
import com.talent.proxy.mysql.common.MysqlExt.SessionStatus;
import com.talent.proxy.mysql.common.MysqlPacket;
import com.talent.proxy.mysql.common.MysqlReturn;
import com.talent.proxy.mysql.common.utils.BufferUtil;

/**
 * <pre>
 *  VERSION 4.1
 *  Bytes                       Name
 *  -----                       ----
 *  1   (Length Coded Binary)   field_count, always = 0
 *  1-9 (Length Coded Binary)   affected_rows
 *  1-9 (Length Coded Binary)   insert_id
 *  2                           server_status
 *  2                           warning_count
 *  n   (until end of packet)   message
 * </pre>
 * 
 */
public class OKPacket extends MysqlPacket
{
	public static final byte MARK = 0x00;
	public static final byte[] OK = new byte[] { 7, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0 };

	public byte fieldCount = MARK;
	public long affectedRows;
	public long insertId;
	public int serverStatus;
	public int warningCount;
	public byte[] message;

	@Override
	public void decodeBody(ByteBuffer buffer, ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{

		int initposition = buffer.position();
		fieldCount = BufferUtil.read(buffer);
		affectedRows = BufferUtil.readLength(buffer);
		insertId = BufferUtil.readLength(buffer);
		serverStatus = BufferUtil.readUB2(buffer);
		warningCount = BufferUtil.readUB2(buffer);

		int usedlength = buffer.position() - initposition;
		if (usedlength < bodySize)
		{
			this.message = BufferUtil.readBytesWithLength(buffer);
		}
	}

	@Override
	public ByteBuffer encode(ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{
		int bodySize = bodySize();
		ByteBuffer buffer = ByteBuffer.allocate(HEADER_SIZE + bodySize);
		BufferUtil.writeUB3(buffer, bodySize);
		buffer.put(seqId);
		buffer.put(fieldCount);
		BufferUtil.writeLength(buffer, affectedRows);
		BufferUtil.writeLength(buffer, insertId);
		BufferUtil.writeUB2(buffer, serverStatus);
		BufferUtil.writeUB2(buffer, warningCount);
		if (message != null)
		{
			BufferUtil.writeWithLength(buffer, message);
		}
		return buffer;
	}

	@Override
	public int bodySize()
	{
		int i = 1;
		i += BufferUtil.getLength(affectedRows);
		i += BufferUtil.getLength(insertId);
		i += 4;
		if (message != null)
		{
			i += BufferUtil.getLength(message);
		}
		return i;
	}

	@Override
	protected String packetDesc()
	{
		return "MySQL OK Packet";
	}

	/** 
	 * @see com.talent.proxy.mysql.common.MysqlPacket#onReceived(com.talent.aio.common.ChannelContext)
	 * 
	 * @param channelContext
	 * @return
	 * @重写人: tanyaowu
	 * @重写时间: 2017年1月23日 下午5:00:19
	 * 
	 */
	@Override
	public MysqlReturn onReceived(ChannelContext<MysqlExt, MysqlPacket, MysqlReturn> channelContext)
	{
		MysqlExt mysqlExt = channelContext.getSessionContext();
		SessionStatus sessionStatus = mysqlExt.getSessionStatus();
		switch (sessionStatus)
		{
		case CLIENT_WAITING_AUTH_RESP:
		{
			mysqlExt.setSessionStatus(SessionStatus.CLIENT_CONNECTED);
			break;
		}
		default:
			break;
		}
		return null;
	}

}
